package com.example.ui;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.Communication;
import com.example.net.NetRequest;
import com.example.service.MainService;
import com.example.ui.contact.chat.SharePreferenceUtil;
import com.example.util.CallBack;
import com.example.util.Util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class LoginActivity extends Activity {
	private Button la_return, la_next;
	private TextView la_title;

	private RelativeLayout la_rlmiss;
	private EditText la_user, la_pwd;
	private ImageView la_clearuser, la_clearpwd;

	private String user, pwd;
	// private ArrayList<Userdata> la_ud;
	public static String userId;
	private Context context;

	public static Handler h;
	public static final int SUCCESS = 1;
	public static final int FAIL = 2;
	SharedPreferences sp;
	private Editor spEditor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_login);
		context = this;
		init();
		h = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case SUCCESS:
					Intent i = new Intent();
					i.setClass(LoginActivity.this, MainActivity.class);
					startActivity(i);
					IndexActivity.isLogin = true;
					spEditor.putBoolean("isAutoLogin", true);
					spEditor.commit();
					finish();
					break;
				case FAIL:
					new AlertDialog.Builder(context).setTitle("提示").setMessage("账号或者密码错误").show();
					break;
				case 3:
					break;
				default:
					break;
				}
				super.handleMessage(msg);
			}
		};

	}
	private void init() {
		la_return = (Button) findViewById(R.id.register_return);
		la_next = (Button) findViewById(R.id.register_next);
		la_title = (TextView) findViewById(R.id.register_title);
		la_next.setText("完成");
		la_title.setText("登录");
		sp=HSApplication.mInstance.getSharedPreferences(HSApplication.SPNAME,HSApplication.MODE_PRIVATE);
		spEditor=sp.edit();
		la_rlmiss = (RelativeLayout) findViewById(R.id.al_rlmiss);
		la_user = (EditText) findViewById(R.id.al_etuser);
		la_pwd = (EditText) findViewById(R.id.al_etpwd);

		la_clearpwd = (ImageView) findViewById(R.id.al_nameclear);
		la_clearuser = (ImageView) findViewById(R.id.al_pwdclear);

		// la_ud=new ArrayList<Userdata>();
		la_return.setOnClickListener(la_ocl);
		la_next.setOnClickListener(la_ocl);
		la_rlmiss.setOnClickListener(la_ocl);
		la_clearuser.setOnClickListener(la_ocl);
		la_clearpwd.setOnClickListener(la_ocl);
	}

	private SharedPreferences getPreferences(String spname, int mode_PRIVATE) {
		
		return null;
	}
	private Builder bulider;
	private AlertDialog infoad;
	private OnClickListener la_ocl = new OnClickListener() {

		

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.register_return:
				Intent i = new Intent();
				i.setClass(LoginActivity.this, IndexActivity.class);
				startActivity(i);
				IndexActivity.isLogin = true;
				finish();
				break;
			case R.id.register_next:
			/*	la_user.setText("18072812823");
				la_pwd.setText("zp1989922");*/
				user = la_user.getText().toString();
				pwd = la_pwd.getText().toString();
				if (user.equals("") || pwd.equals("")) {
					Util.toast(LoginActivity.this, "账户或者密码不能为空");
				} else {
					if(NetRequest.isOpenNetwork(context)==true){
					PushManager.startWork(context,
							PushConstants.LOGIN_TYPE_API_KEY,
							HSApplication.API_KEY);// 无baidu帐号登录 Leone add
					/*user="18072812823";
					pwd="zp1989922";*/
					bulider = new AlertDialog.Builder(context);
					bulider.setView(View.inflate(context, R.layout.onloading, null))
					.create();
					infoad = bulider.show();
					Communication.login(user, pwd,new CallBack() {
						
						@Override
						public void run(Boolean isError, Object data) {
							
							if(isError){
								if("true".equals(data)){
											Intent i = new Intent();
											i.setClass(LoginActivity.this, MainActivity.class);
											startActivity(i);
											IndexActivity.isLogin = true;
											spEditor.putBoolean("isAutoLogin", true);
											spEditor.commit();
											finish();
								}else if("false".equals(data)){
									 new AlertDialog.Builder(LoginActivity.this).setTitle("提示！您的账号已过期请重新续费")
									  .setMessage("请联系客服！")
					        		 .setPositiveButton("确定",
											new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
											infoad.dismiss();
										}
									}).show();
								}
							}else if("faile".equals(data)){
									 new AlertDialog.Builder(LoginActivity.this).setTitle("提示登陆失败")
									  .setMessage("账号或者密码错误")
					        		 .setPositiveButton("确定",
											new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
										}
									}).show();
							}else{
								new AlertDialog.Builder(LoginActivity.this).setTitle("提示数据异常")
								  .setMessage("请联系技术人员！")
				        		 .setPositiveButton("确定",
										new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
									}
								}).show();
							}
						}
						
					});
					}else{
						new AlertDialog.Builder(context).setTitle("提示").setMessage("请检查网络连接").show();
						
					}
					infoad.dismiss();
				}
				break;
			case R.id.al_rlmiss:// 忘记密码
				startActivity(new Intent(LoginActivity.this,
						FindPassword1Activity.class));
				break;
			case R.id.al_nameclear:
				la_user.setText("");
				break;
			case R.id.al_pwdclear:
				la_pwd.setText("");
				break;
			default:
				break;
			}
		}
	};
}
